<?php
namespace Application\QQMailer;
use PHPMailer\PHPMailer\PHPMailer;
require_once VENDOR_PATH.'/phpmailer/phpmailer/src/Exception.php';
require_once VENDOR_PATH. '/phpmailer/phpmailer/src/PHPMailer.php';
require_once VENDOR_PATH. '/phpmailer/phpmailer/src/SMTP.php';
class QQMailer{
    protected $config = [];
    protected $mailer ;
    public function __construct()
    {
        $this->config = Yaf_Registry::get('config');
        $this->mailer = new PHPMailer();
        $this->mailer->SMTPDebug = $this->config->qq->smtpdebug;
        $this->mailer->isSMTP(); // 使用 SMTP 方式发送邮件
    }
    /**
     * @return PHPMailer
     */
    public function getMailer()
    {
        return $this->mailer;
    }
    /**
     *
     */
    private function loadConfig()
    {
        /*Server Setting */
        $this->mailer->SMTPAuth = true;                     //开启smtp认证
        $this->mailer->Host = $this->config->qq->host;      //smtp服务器地址
        $this->mailer->Port = $this->config->qq->prot;      //port 远程服务端口
        $this->mailer->SMTPSecure = $this->config->qq->encryption;      //加密方式
        /*Account Setting*/
        $this->mailer->Username = $this->config->qq->username;  //smtp 登录帐号
        $this->mailer->Password = $this->config->qq->password;  //smtp 登录密码（qq授权码）
        $this->mailer->From = $this->config->qq->username;      //发件人邮箱
        $this->mailer->FromName = $this->config->qq->username;  //发件人昵称
        /*Content Setting*/
        $this->mailer->isHTML(true);                      //邮件正文是否为html
        $this->mailer->CharSet = $this->config->qq->charset;      //发送邮件编码
    }

    /***
     * @param $path 附件路径
     *
     * @throws \PHPMailer\PHPMailer\Exception
     */
    public function addFile($path)
    {
        $this->mailer->addAttachment($path);
    }

    /***
     * Send Email
     * @param $email 收件人邮箱
     * @param $title  邮件主题
     * @param $content 邮件信息
     * @return bool 邮件状态
     * @throws \PHPMailer\PHPMailer\Exception
     */
    public function send($email, $title, $content)
    {
        $this->loadConfig();

        $this->mailer->addAddress($email);       //收件人邮箱
        $this->mailer->Subject = $title;         //邮件主题
        $this->mailer->Body = $content;          //邮件信息
        return (bool)$this->mailer->send();      //发送邮件
    }
}